home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / tools / gimppath2svg.py < prev    next >
Encoding:
Python Source  |  2000-10-03  |  3.2 KB  |  120 lines

  1. #!/usr/bin/env python
  2.  
  3. import sys,re
  4.  
  5. """
  6. gimppath2svg.py -- Converts Gimp-Paths to a SVG-File.
  7. Copyright (C) 2000  Simon Budig <simon@gimp.org>
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23.  
  24. Usage: gimppath2svg.py [infile [outfile]]
  25. """
  26.  
  27.  
  28. svgtemplate = """<?xml version="1.0" standalone="yes"?>
  29. <svg width="%d" height="%d">
  30. <g>
  31. <path id="%s" transform="translate (%d,%d)"
  32.       style="stroke:#000000; stroke-width:1; fill:none"
  33.       d="%s"/>
  34. </g>
  35. </svg>
  36. """
  37.  
  38. emptysvgtemplate = """<?xml version="1.0" standalone="yes"?>
  39. <svg width="1" height="1">
  40. </svg>
  41. """
  42.  
  43.  
  44. class Path:
  45.    def __init__(self):
  46.       self.name = ""
  47.       self.svgpath = ""
  48.       self.gimppoints = [[]]
  49.       self.bounds = None
  50.    
  51.    def readgimpfile (self, filedesc):
  52.       text = filedesc.readlines()
  53.       for line in text:
  54.      namematch = re.match ("Name: (.*)$", line)
  55.      if namematch:
  56.         path.name = namematch.group(1)
  57.      pointmatch = re.match ("TYPE: (\d) X: (\d+) Y: (\d+)", line)
  58.      if pointmatch:
  59.         if pointmatch.group (1) == "3":
  60.            path.gimppoints.append ([])
  61.         (x, y) = map (int, pointmatch.groups()[1:])
  62.         path.gimppoints[-1].append (map (int, pointmatch.groups()))
  63.         if self.bounds:
  64.            if self.bounds[0] > x: self.bounds[0] = x
  65.            if self.bounds[1] > y: self.bounds[1] = y
  66.            if self.bounds[2] < x: self.bounds[2] = x
  67.            if self.bounds[3] < y: self.bounds[3] = y
  68.         else:
  69.            self.bounds = [x,y,x,y]
  70.    
  71.    def makesvg (self):
  72.       for path in self.gimppoints:
  73.          if path:
  74.         start = path[0]
  75.         svg = "M %d %d " % tuple (start[1:])
  76.         path = path[1:]
  77.         while path:
  78.            curve = path [0:3]
  79.            path = path[3:]
  80.            if len (curve) == 2:
  81.           svg = svg + "C %d %d %d %d %d %d z " % tuple (
  82.                tuple (curve [0][1:]) + 
  83.                tuple (curve [1][1:]) + 
  84.                tuple (start [1:]))
  85.            if len (curve) == 3:
  86.           svg = svg + "C %d %d %d %d %d %d " % tuple (
  87.                tuple (curve [0][1:]) + 
  88.                tuple (curve [1][1:]) + 
  89.                tuple (curve [2][1:]))
  90.         self.svgpath = self.svgpath + svg
  91.  
  92.    def writesvgfile (self, outfile):
  93.       if self.svgpath:
  94.      svg = svgtemplate % (self.bounds[2]-self.bounds[0],
  95.                   self.bounds[3]-self.bounds[1],
  96.                   self.name,
  97.                   -self.bounds[0], -self.bounds[1],
  98.                   self.svgpath)
  99.       else:
  100.          svg = emptysvgtemplate
  101.       outfile.write (svg)
  102.  
  103.  
  104. if len (sys.argv) > 1:
  105.    infile = open (sys.argv[1])
  106. else:
  107.    infile = sys.stdin
  108.  
  109. if len (sys.argv) > 2:
  110.    outfile = open (sys.argv[2], "w")
  111. else:
  112.    outfile = sys.stdout
  113.  
  114.  
  115. path = Path()
  116. path.readgimpfile (infile)
  117. path.makesvg()
  118. path.writesvgfile (outfile)
  119.  
  120.